class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words)
    {
        vector<int> ret;
        unordered_map<string, int> hash1;
        for(auto c : words) hash1[c]++;

        int len = words[0].size();
        int m = words.size();
        int size = s.size();

        for(int i = 0; i < len; i++)
        {
            unordered_map<string, int> hash2;
                            // 细节问题，right入窗口时要保证right + len是有效的
            for(int left = i, right = i, count = 0; right + len <= size; right += len)
            {
                // 入窗口 维护count 有效字符串个数
                string in = s.substr(right, len);
                //hash2[in]++;
                if(++hash2[in] <= hash1[in]) count++;
                // 判断
                if(right - left + 1 > len * m)
                {
                    string out = s.substr(left, len);
                    // 出窗口 维护count
                    if(hash2[out]-- <= hash1[out]) count--;
                    //hash2[out]--;
                    left += len;
                }
                // 更新结果
                if(count == m) ret.push_back(left);
            }
        }
        return ret;

    }
};
